<?php
class ModelsController extends StoreAppController {

	var $name = 'StoreModels';
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'view' => array('admin','manag')
	);
	public function beforeFilter(){
		parent::beforeFilter();
		App::import('Model','Store.StoreCategory'); 
		$this->StoreCategory = new StoreCategory();
		
//		$this->layout = 'cms';
	}
	
	function index() {
		$this->StoreModel->recursive = 0;
		$this->set('storeModels', $this->paginate());		
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store model', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('storeModel', $this->StoreModel->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->StoreModel->create();
			if ($this->StoreModel->save($this->data)) {
				$this->Session->setFlash(__d('store','The store model has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store model could not be saved. Please, try again.', true));
			}
		}
		$parentCategory = $this->StoreCategory->find('threaded', array());
		$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');
		$this->set('storeCategories',$parentCategories);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store model', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->StoreModel->save($this->data)) {
				$this->Session->setFlash(__d('store','The store model has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__d('store','The store model could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->StoreModel->read(null, $id);
			$parentCategory = $this->StoreCategory->find('threaded', array());
			$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');
			$this->set('storeCategories',$parentCategories);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store model', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->StoreModel->delete($id)) {
			$this->Session->setFlash(__d('store','Store model deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__d('store','Store model was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>